<template>
  <div class="login-container">
    <div class="login-part">
      <div class="login-img">
        <img src="../assets/logo.png" alt="">
      </div>
      <el-form ref="loginFormRef" :model="loginForm" :rules="loginRuls" label-width="0">
        <el-form-item prop="username">
          <el-input prefix-icon="iconfont iconusercenter" v-model="loginForm.username"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input prefix-icon="iconfont iconpassword" v-model="loginForm.password" type="password"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
  import {login} from '../network/home'

  export default {
    name: "Login",
    data() {
      return {
        loginForm: {
          username: 'admin',
          password: '123456'
        },
        loginRuls: {
          username: [
            {required: true, message: '请输入名称', trigger: 'blur'},
            {min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur'}
          ],
          password: [
            {required: true, message: '请输入密码', trigger: 'blur'},
            {min: 5, max: 15, message: '长度在 5 到 15 个字符', trigger: 'blur'}
          ]
        }
      }
    },
    methods: {
      reset() {
        this.$refs.loginFormRef.resetFields();
      },
      login() {
        this.$refs.loginFormRef.validate(valid => {
          if (valid) {
            login(this.loginForm).then(res => {
              console.log(res);
              if (res.meta.status !== 200) return this.$message.error("登录失败")
              window.sessionStorage.setItem("token", res.data.token)
              this.$message.success("登录成功")
              this.$router.push("/home")
            })
          }
        })
      }
    }
  }
</script>

<style lang="less" scoped>
  .login-container {
    background-color: #2b4b6b;
    height: 1000%;
    width: 100%;
  }

  .login-part {
    width: 450px;
    height: 400px;
    background-color: #fffdef;
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);

    .login-img {
      width: 100px;
    }
  }
</style>
